#!/usr/bin/env python
#
#       activitydefinitions.py
#
#       Copyright 2009 karthikeyan S. <kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import wx
import sys
import unittest

import accessories

from resultdefinitions import PyStats

class ActivityEvents:

    def __init__(self,baseframe):
        self.Inst=baseframe

    def run(self,event):
        """
        Run the generated testcases
        """
        sys_stdout= open("PyTestTemp.log","a")
        original_stdout = sys.stdout
        sys.stdout = sys_stdout

        stats=[]
        rmod = None
        module=str(self.Inst.modname.GetValue())

        if module and os.path.isfile("pytestgen_%s.py"%module) :
            rmod=__import__("pytestgen_%s"%module)
        else :
            self.newfileval=accessories.textentry\
                (self.Inst,"Enter the path of the generated file")
            if not self.newfileval == None and "/" in self.newfileval :
                self.newfileval=self.newfileval.split("/")
                rmod=__import__(self.newfileval[len(self.newfileval)-1])
            elif not self.newfileval == None and len(self.newfileval) >= 1:
                rmod=__import__(self.newfileval)
        try :
            testlist=unittest.TestSuite()
            testlist.addTest(unittest.makeSuite(rmod.PyUnitframework))
            result = unittest.TextTestRunner(verbosity=0).run(testlist)
        except Exception,E :
            print E
            accessories.infobox(self.Inst,\
                "Invalid PyTest Generated file","Run",wx.ICON_WARNING)

        sys.stdout = original_stdout
        sys_stdout.close()
        os.unlink("PyTestTemp.log")

        if rmod :
            stats.append(rmod.__name__.split("_")[1])
            stats.append(result.testsRun)
            stats.append([len(result.failures),result.failures])
            stats.append([len(result.errors),result.errors])

            result=open("PyTestResults.txt",'w')
            result.write("Module : %s\n"%stats[0])
            result.write("Number of Cases : %s\n\n"%stats[1])
            result.write("Failures : %s\n"%stats[2][0])
            result.write("%s\n\n"%stats[2][1])
            result.write("Errors : %s\n"%stats[3][0])
            result.write("%s\n"%stats[3][1])
            del result

            cstat = PyStats(self.Inst, 1, 'PyTestGenerator Results',stats)
            cstat.Centre()
            cstat.Show(True)
